package com.example.myapplication.SuiPian;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.Re_Servlet;
import com.example.myapplication.apter.CatrApter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;


public class HelpFragment extends Fragment {


    private View view;
    private RecyclerView rcy_service;
    CatrApter serverApter;
    List<Re_Servlet.RowsDTO> rows = new ArrayList<>();
    private SearchView search;
    private String TAG = "TAG";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_help, container, false);
        initview();
        //全部服务
        getCarDate();

        return view;
    }

    private void getcontent( ) {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 当用户点击搜索按钮时触发
                // 在这里处理搜索逻辑
                return true;
            }

          /*  @Override
            public boolean onQueryTextChange(String newText) {
                // 当用户输入搜索关键字时触发
                // 在这里处理搜索关键字变化的逻辑

                //0:判断文本长度0：退出搜素，长度大于0的时候，进入搜索
                if(TextUtils.isEmpty(newText)){
                    serverApter.setList(rows);
                    serverApter.notifyDataSetChanged();
                }else{
                    //1比对，生成一个新的list
                    //2把新的list设置给adapter，然后刷新。
                    List<Re_Servlet.RowsDTO> row2 = new ArrayList<>();
                    for(Re_Servlet.RowsDTO bean:rows){
                        if(bean.getServiceName().equals(newText)){
                            row2.add(bean);
                        }
                    }
                    serverApter.setList(row2);
                    serverApter.notifyDataSetChanged();
                }
                return true;
            }*/


            // toLowerCase()  使用默认语言环境的规则将此 String 中的所有字符都转换为小写
            // contains 当且仅当此字符串包含指定的 char 值序列时，返回 true。
            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    // 如果搜索关键字为空，显示所有数据
                    serverApter.setList(rows);
                } else {
                    // 如果搜索关键字不为空，进行部分字符串匹配
                    List<Re_Servlet.RowsDTO> filteredRows = new ArrayList<>();
                    for (Re_Servlet.RowsDTO bean : rows) {
                        if (bean.getServiceName().toLowerCase().contains(newText.toLowerCase())) {
                            filteredRows.add(bean);
                        }
                    }
                    serverApter.setList(filteredRows);
                }
                serverApter.notifyDataSetChanged();

                return true;
            }
        });
    }

    private void initview() {
        rcy_service = view.findViewById(R.id.rcy_service);
        search = view.findViewById(R.id.search);
    }


    private void getCarDate() {
        OKHttpTool.doNetRequest("prod-api/api/service/list",
                OKHttpTool.GET,
                Re_Servlet.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(HelpFragment.this.getActivity(), new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                Re_Servlet re_servlet = (Re_Servlet) obj;
                                if (re_servlet.getCode() == 200) {
                                    binServer(re_servlet);
                                    //搜索
                                    getcontent();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    private void binServer( Re_Servlet re_servlet) {
        //律师
        Re_Servlet.RowsDTO rowsDTO1 = new Re_Servlet.RowsDTO();
        rowsDTO1.setServiceName("律师");
        rowsDTO1.setId(99);
        rowsDTO1.setImgUrl("/prod-api/profile/upload/image/2023/02/13/081db43b-4b53-4261-968e-b75c52477f63.png");

        //预约检车
        Re_Servlet.RowsDTO rowsDTO2 = new Re_Servlet.RowsDTO();
        rowsDTO2.setServiceName("预约检车");
        rowsDTO2.setId(88);
        rowsDTO2.setImgUrl("/prod-api/profile/upload/image/2023/02/13/081db43b-4b53-4261-968e-b75c52477f63.png");

        //智慧养老
        Re_Servlet.RowsDTO rowsDTO3 = new Re_Servlet.RowsDTO();
        rowsDTO3.setServiceName("智慧养老");
        rowsDTO3.setId(188);
        rowsDTO3.setImgUrl("/prod-api/profile/upload/image/2023/02/13/081db43b-4b53-4261-968e-b75c52477f63.png");


        rows.add(rowsDTO1);
        rows.add(rowsDTO2);
        rows.add(rowsDTO3);


        //适配器
        //ridLayoutManager 是 Android 中的一种布局管理器，用于在 RecyclerView 中展示网格布局。它将项目以网格状的形式进行排列，并提供了灵活的跨行和跨列功能。
        serverApter = new CatrApter(getContext(), re_servlet.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 5);
        rcy_service.setLayoutManager(gridLayoutManager);
        rcy_service.setAdapter(serverApter);
    }
}